package it.yuzuojian.com.mapper;

import it.yuzuojian.com.javaBean.Article;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;

import javax.naming.Name;
import java.util.List;

@Mapper
@Repository
public interface ArticleMapper {




    @Delete("delete from article where id=#{id}")
    int deleteById(Integer id);

    @Insert("insert into article values(null,#{title}, " +
            " #{content},#{viewCount}," +
            " #{likeCount},#{appreciateEable},#{shareEable}, " +
            " #{commentEable1},#{typeName},#{publish}, " +
            " #{createTime},#{updateTime},#{recommend},#{flag},#{description},#{firstPicture})")
    void add(Article article);

    @Select("select * from article")
    List<Article> findAll();

    /*@Select("select article.* ,tags.id as tagid ,tags.name as tagname " +
            " from article,tags,articletags  " +
            " where article.id=articletags.articleid and tags.id=articletags.tagid and article.id=#{id}")*/
    @Select("select * from article where id=#{id}")
    @Results(id = "articleTags111",value =
            {@Result(column = "id", property = "id", id = true),
                    @Result(column = "title", property = "title"),
                    @Result(column = "viewCount", property = "viewCount"),
                    @Result(column = "likeCount", property = "likeCount"),
                    @Result(column = "appreciateEable", property = "appreciateEable"),
                    @Result(column = "commentEable1", property = "commentEable1"),
                    @Result(column = "shareEable", property = "shareEable"),
                    @Result(column = "typeName", property = "typeName"),
                    @Result(column = "publish", property = "publish"),
                    @Result(column = "createTime", property = "createTime"),
                    @Result(column = "updateTime", property = "updateTime"),
                    @Result(column = "content", property = "content"),
                    @Result(column = "recommend", property = "recommend"),
                    @Result(column = "flag", property = "flag"),
                    @Result(column = "description", property = "description"),
                    @Result(column = "firstPicture", property = "firstPicture"),
                    @Result(column = "id", property = "tagsList",javaType = List.class,
                            many = @Many(select = "it.yuzuojian.com.mapper.TagsMapper.findByIdInArticleTags", fetchType = FetchType.LAZY))})
    /*@ResultMap("articleTags111")*/
    Article findById(Integer id);

    @Select({"<script>" +
            "select * from article"
            + "<where>"
            + " 1=1 "
            + "<if test= 'typeName!=null'>"
            + "and typeName=#{typeName} "
            + " </if>"
            + " <if test='title!=null'>"
            + " and title like #{title} "
            + " </if>"
            + " <if test='recommend!=null'>"
            + " and recommend=#{recommend} "
            + " </if>"
            + " </where>"
            + " </script>"})
    List<Article> searchArticle(@Param("typeName") String typeName, @Param("title") String title, @Param("recommend") Boolean recommend);

    @Update(" update article set title=#{title},content=#{content},viewCount=#{viewCount}," +
            " likeCount=#{likeCount},appreciateEable=#{appreciateEable},shareEable=#{shareEable}, " +
            " typeName= #{typeName},publish=#{publish},updateTime=#{updateTime},recommend=#{recommend}, " +
            " commentEable1=#{commentEable1},flag=#{flag},description=#{description},firstPicture=#{firstPicture} " +
            "  where id=#{id}")
    void update(Article article);


    @Select("SELECT MAX(article.id) FROM article;")
    Integer findMaxId();
    @Select("select count(1) from article")
    Integer findCounts();
    @Select("select * from article where recommend=true ORDER BY createTime desc limit 0,6")
    List<Article> findRecommendNewArticle();

    @Select("select * from article where typeName=#{typeName}")
    List<Article> findByTypeName(String typeName);

    @Select("select * from article where id in(select articleId from articletags where tagId=#{tagId})")
    @ResultMap("articleTags111")
    List<Article> findByTagId(Integer tagId);

    @Update("update article set viewcount=viewCount+1 where id=#{id}")
    void addViewCount(Integer id);

    @Select("select * from article where title like #{query} or content like #{query}")
    List<Article> search(String query);
    /*@Select("select count(1) from article where title like #{query} or content like #{query}")
    Integer searchCounts(String query);*/

    @Update("update article set viewcount=#{viewCount} where id=#{id}")
    void updateViewCount(@Param("id") Integer id,@Param("viewCount") Integer viewCount);
}